package temp;

import java.util.HashSet;
import java.util.Set;

/**
 * @author wangxi created on 2021/8/8 18:50
 * @version v1.0
 */
public class TreeMerge {

    public void merge(Set<Node> set1, Set<Node> set2) {
        if ((set1 == null || set1.size() == 0) && (set2 == null || set2.size() == 0)) {
            return;
        }
        if (set1 == null || set1.size() == 0) {
            set1 = set2;
            return;
        }
        if (set2 == null || set2.size() == 0) {
            return;
        }
        set1.addAll(set2);
        while (set1.iterator().hasNext() || set2.iterator().hasNext()) {

            merge(set1.iterator().next().set, set2.iterator().next().set);
        }
    }

    public class Node {
        char c;
        Set<Node> set;

        public Node(){}

        public Node(char c) {
            this.c = c;
            set = new HashSet<>();
        }

        @Override
        public int hashCode() {
            return (int)c;
        }

        @Override
        public boolean equals(Object obj) {
            if (!(obj instanceof Node)) {
                return false;
            }
            Node node = (Node)obj;
            return node.c == this.c;
        }
    }
}
